<template>
	<view class="" style="padding: 40rpx;">
		<u--form :model="form" ref="form">
			<u-form-item label="企业名称" prop="name">
				<input class="input" v-model="form.name" />
			</u-form-item>

			<u-form-item label="企业地址" prop="address">
				<input class="input" v-model="form.address" />
			</u-form-item>
		</u--form>


		<view class="bt">
			<u-button type="primary" @click="submit">保存</u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				companyId: "",
				form: {
					id: "",
					name: "",
					address: ""
				}
			}
		},
		methods: {

			employee() {
				var that = this
				this.api.post("/employee/register/", {
					userId: that.$store.state.userId,
					companyId: that.companyId,
					sex: 1,
					userName: that.form.name + "管理员",
					phone: "未完善",

				}).then(res => {
					console.log(res)
					if (res.code != 200) {
						this.$refs.uToast.show({
							duration:1000,
							type: 'error',
							icon: false,
							title: '提示',
							message: res.mes,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
						})
					} else {
						that.$store.state.employeeId = res.data.id
						that.$refs.uToast.show({
							duration:1000,
							type: 'default',
							title: '提示',
							message: res.mes,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/default.png'
						})



						setTimeout(function() {
							uni.hideLoading();
							that.$store.commit("getUserInfo")
							
							let routes = getCurrentPages()
							let curRoute = routes[routes.length - 2].route
							console.log(curRoute)
							if(curRoute=="pages/mycompany/mycompany"){
								uni.navigateBack({
									
								})
								
							}else{
								uni.navigateTo({
									url: "/pages/login/login"
								})
							}
							
						}, 1500);

					}

				})
			},



			submit() {

				if (this.form.name == "" && this.form.address == "") {
					this.$refs.uToast.show({
						duration:1000,
						type: 'default',
						title: '提示',
						message: "请完善信息！",
					})
					return
				}

				var that = this
				this.api.post("/User/bossRegister/", {
					id:that.form.id,
					name: that.form.name,
					address: that.form.address,
					ownerUserId: that.$store.state.userId
				}).then(res => {
					that.companyId = res.data.id
					that.indexList = res.data
					console.log(res);
					that.$refs.uToast.show({
						duration:1000,
						type: 'default',
						title: '提示',
						message: res.mes,
					})

					this.employee()


				})

			}
		},
		onLoad(options) {
			var that = this
			if (Object.keys(options).length != 0) {
				this.api.get("/company/findInfo/" + options.id, {}).then(res => {
					that.form.name = res.data.name
					that.form.address = res.data.address
					that.form.id = options.id
				})
			}
		}
	}
</script>

<style scoped>
	.input {
		border-bottom: 1rpx #BBBCC6 solid;

	}

	.bt {
		width: 95%;
		margin: 20rpx auto;
	}
</style>
